<?php

class CS_Application extends CS_Core
{
	public $paths = array('core' => null, 'theme' => null);

	public $base = false;

	public $controller;
	public $model;
	public $view;

	public function init($controller = null, $action = null, $args = null)
	{
		// initiate the application

		// Load the confitguration file
		$this->_loadConfig();

		// TODO : allow to set custom paths
		$this->setPaths();

		// load the necessary controller
		$this->_loadController($controller);

		// process all request/get/post arguments
		$this->_processRequest();

		$this->controller->beforeFilter();

		$this->setTheme($this->controller->theme);

		$this->_loadModel();

		$this->_loadView($action);

		switch(count($args)) { 
			case 0: $this->controller->{$action.'Action'}(); break; 
			case 1: $this->controller->{$action.'Action'}($args[3]); break; 
			case 2: $this->controller->{$action.'Action'}($args[3], $args[4]); break; 
			case 3: $this->controller->{$action.'Action'}($args[3], $args[4], $args[5]); break; 
			case 4: $this->controller->{$action.'Action'}($args[3], $args[4], $args[5], $args[6]); break; 
			case 5: $this->controller->{$action.'Action'}($args[3], $args[4], $args[5], $args[6], $args[7]); break; 
			default: call_user_func_array(array($this->controller, $action.'Action'), $args);  break; 
		}

		$this->controller->afterFilter();

		$this->view->setVars($this->view->vars);

		$this->view->setTheme($this->controller->theme);

		$this->view->loadView($this->controller->name, $action);

		$this->controller->beforeRender($this->view->content);

		$this->view->loadLayout($this->controller->layout);

		$this->controller->afterRender();
	}

	public function setPaths()
	{		
		$this->paths['core'] = PATH.'core'.DS;
		$this->paths['theme'] = PATH.'design'.DS.'default';
	}

	public function path($type = null)
	{

		if(empty($type))
		{
			return null;
		}

		if(isset($this->paths[$type]) && !empty($this->paths[$type]))
		{
			return $this->paths[$type];
		} else {
			try{
				throw new CS_Error_Exception('Invalid Path specified - '.$type);
			} catch(CS_Error_Exception $e)
			{
				$e->printError();
			}
		}
	}

	public function setTheme($theme = null)
	{
		if(empty($theme))
		{
			// set default theme is no custom theme exists
			$theme = 'default';
		}

		$this->paths['theme'] = $this->paths['theme'] = PATH.'design'.DS.$theme;
	}

	public function _loadConfig()
	{
		$config = new CS_Config_Xml(CONFIG.DS.'application.xml');
	}

	public static function baseUrl($type = null)
	{
		if(empty($type))
		{
			$baseUrl = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
		} else {
			switch($type)
			{
				case 'js' : 
					$baseUrl = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']).'web/js/';
				break;

				case 'css' : 
					$baseUrl = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']).'web/js/';
				break;

				case 'img' : 
					$baseUrl = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']).'web/js/';
				break;
			}
		}

		return $baseUrl;
	}

	private function _processRequest()
	{

		// TODO : sanitize post and get data
		if(isset($_REQUEST) && !empty($_REQUEST))
		{
			$this->data =& $_REQUEST;
		}

		if(isset($_POST) && !empty($_POST))
		{
			$this->data =& array_merge($this->data, $_GET);
		}

		$this->controller->data =& $this->data;
	}
}
